package com.alp.scan.common;

import android.content.Context;
import android.content.Intent;
import android.hardware.Camera;
import android.net.Uri;
import android.os.Build;
import android.widget.EditText;

public class EditTextCommon {

    /**
     * 将焦点放在输入框中并选中全部 如果想要选中输入框中的文本必须要将焦点放在输入框中 如果想要焦点在输入框中必须设置下面三个方法
     *
     * @param editText
     */
    public static void setEditFocus(EditText editText) {
        editText.setFocusable(true);
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();
        editText.selectAll();
    }

    public static void setEditFocus(Context context, EditText editText) {

        editText.setFocusable(true);
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();
        editText.selectAll();
    }

    /**
     * 扫描条码格式为：http://tm.tengen.com/?A@7323409@42732@16@140611CMW2TN@TM00101 提取出
     * 140611CMW2TN，如果用"@"分割后的数组长度小于5，则不分割。
     *
     * @param strBarCode
     * @return
     */
    public static String BarCodeFormat(String strBarCode) {
        String[] splits = strBarCode.split("@");
        if (splits.length >= 5) {
            return splits[4];
        } else {
            return strBarCode;
        }
    }

    public static int GetBarCodeQty(String strBarCode) {
        int i = 1;
        try {
            String[] splits = strBarCode.split("@");
            if (splits.length >= 5) {
                i = Integer.parseInt(splits[3]);
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
        return i;

    }

    public static boolean isNumber(String str) {// 判断整型
        return str.matches("[//d]+");
    }

    /**
     * 半角转换为全角
     *
     * @param input
     * @return
     */
    public static String ToDBC(String input) {
        char[] c = input.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if (c[i] == 12288) {
                c[i] = (char) 32;
                continue;
            }
            if (c[i] > 65280 && c[i] < 65375) {
                c[i] = (char) (c[i] - 65248);
            }
        }
        return new String(c);
    }

    public interface OnChangeListener {

        void onChange(int Qty);
    }

    public static boolean booleanisCameraUseable() {

        boolean canUse = true;
        Camera mCamera = null;
        try {
            mCamera = Camera.open(1);
            // setParameters 是针对魅族MX5。MX5通过Camera.open()拿到的Camera对象不为null
            Camera.Parameters mParameters = mCamera.getParameters();
            mCamera.setParameters(mParameters);
        } catch (Exception e) {
            canUse = false;
        }
        if (mCamera != null) {
            mCamera.release();
        }
        return canUse;
    }

    public static void getAppDetailSettingIntent(Context context) {
        Intent localIntent = new Intent();
        localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= 9) {
            localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
            localIntent.setData(Uri.fromParts("package", context.getPackageName(), null));
        } else if (Build.VERSION.SDK_INT <= 8) {
            localIntent.setAction(Intent.ACTION_VIEW);
            localIntent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
            localIntent.putExtra("com.android.settings.ApplicationPkgName", context.getPackageName());
        }
        context.startActivity(localIntent);
    }
}
